﻿using aier.dataaccess.dapper;
using aier.dataaccess.model;
using aier.xrm.wcf;
using log4net;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.business.logic
{
    public class ContactBll
    {
        public ILog log { get; set; }
        public ResResult resResult { get; set; }
        public CommonWcf commonWcf { get; set; }
        public ContactDal contactDal { get; set; }


        /// <summary>
        /// 添加联系人
        /// </summary>
        /// <param name="contactInfo"></param>
        /// <returns></returns>
        public ResResult AddContact(ContactInfo contactInfo)
        {
            Guid resultId = Guid.Empty;
            try
            {
                if (contactInfo == null)
                {
                    log.Error("ModContact 出现错误：传入实体为空");
                    return resResult.FailResult("ContactBll ->AddContact : 传入实体为空");
                }

                Entity entity = contactInfo.AsCrmEntity();
                if (contactInfo.userid == Guid.Empty)
                {
                    resultId = commonWcf.CreateEntity(entity);
                }
                else
                {
                    resultId = commonWcf.CreateEntityProxy(contactInfo.userid, entity);
                }

                return resResult.SuccessResult(resultId);
            }
            catch (Exception e)
            {
                log.Error("AddContact 出现错误：" + e.Message);
                return resResult.FailResult("ContactBll ->AddContact Error:" + e.Message);

            }

        }

        /// <summary>
        /// 删除联系人
        /// </summary>
        /// <param name="contactId"></param>
        public ResResult DelContact(Guid contactId) {
            try
            {
                if (contactId == Guid.Empty)
                {
                    log.Error("DelContact 出现错误：传入id为空");
                    return resResult.FailResult("ContactBll ->DelContact : 传入id为空");
                }
                commonWcf.DeleteEntityById("contact", contactId);
               
                return resResult.SuccessResult(contactId); 
            }
            catch (Exception e)
            {
                log.Error("DelContact 出现错误：" + e.Message);
                return resResult.FailResult("ContactBll ->DelContact : " + e.Message);
            }
           
        }

        /// <summary>
        /// 更新联系人
        /// </summary>
        /// <param name="contactInfo"></param>
        public ResResult ModContact(ContactInfo contactInfo) {
            try
            {
                if (contactInfo == null||Guid.Empty.Equals( contactInfo.Contactid))
                {
                    log.Error("ModContact 出现错误：传入实体为空" );
                    return  resResult.FailResult("ContactBll ->ModContact : 传入实体为空或ID为空");
                }
                if (null == contactDal.QryContact(contactInfo.Contactid))
                {
                    return resResult.FailResult("ContactBll ->ModContact : 不存在该联系人");
                }
                Entity entity = contactInfo.AsCrmEntity();
                if (contactInfo.userid==Guid.Empty)
                {
                    commonWcf.UpdateEntityById(entity);
                }
                else
                {
                    commonWcf.UpdateEntityProxyById(contactInfo.userid, entity);
                }

                return resResult.SuccessResult(contactInfo.Contactid);
            }
            catch (Exception e)
            {
                log.Error("ModContact 出现错误：" + e.Message);
                return resResult.FailResult("ContactBll ->ModContact :" + e.Message);
            }
        }

        /// <summary>
        /// 根据联系人id查询联系人
        /// </summary>
        /// <param name="contactId"></param>
        /// <returns></returns>
        public ContactView QryContact(Guid contactId) {

            return contactDal.QryContact(contactId);
        }

        /// <summary>
        /// 根据区域、负责人、合作意向、活跃度、开始时间、结束时间查询联系人(时间格式：YYYY-MM-DD)
        /// </summary>
        /// <param name="new_districtandcounty">区域</param>
        /// <param name="new_liveness">活跃度</param>
        /// <param name="new_cooperativeintention">合作意向</param>
        /// <param name="startDate">开始时间(YYYY-MM-DD)</param>
        /// <param name="endDate">结束时间(YYYY-MM-DD)</param>
        /// <param name="ownerid">负责人</param>
        /// <returns></returns>
        public IEnumerable<ContactView> QryContactList(Guid new_districtandcounty, int? new_liveness
                    , bool? new_cooperativeintention, string startDate, string endDate, Guid ownerid)
        {
            
              return contactDal.QryContactList(new_districtandcounty, new_liveness, new_cooperativeintention, startDate, endDate, ownerid);
        }

        /// <summary>
        /// 根据区域和负责人查询联系人
        /// </summary>
        /// <param name="new_districtandcounty">区域</param>
        /// <param name="new_liveness">活跃度</param>
        /// <param name="ownerid">负责人</param>
        /// <returns></returns>
        public IEnumerable<ContactView> QryContactList(Guid new_districtandcounty, int? new_liveness, Guid ownerid)
        {

            return QryContactList(new_districtandcounty, new_liveness,null,null,null, ownerid);
        }

        /// <summary>
        /// 根据姓名、负责人查询联系人
        /// </summary>
        /// <param name="name">姓名</param>
        /// <param name="ownerid">负责人</param>
        /// <returns></returns>
        public IEnumerable<ContactView> QryContactListByNameAndOwnerId(string name, Guid ownerid) {
            
            return contactDal.QryContactListByNameAndOwnerId(name, ownerid);
        }

        /// <summary>
        /// 根据负责人查询区域下拉列表
        /// </summary>
        /// <param name="ownerid">负责人</param>
        /// <param name="type"></param>
        /// <returns></returns>
        public IEnumerable<ContactView> QryDistrictandcountyList(Guid ownerid, int? type) {
           
              return contactDal.QryDistrictandcountyList(ownerid, type);
        }
    }
}
